﻿<%@page language="java" contentType="text/html" pageEncoding="UTF-8"%>

<script>
	$(function(){
		$("#txtUsername").focus();
		$("#btnLogin").click(function(){
			$.login();
		});
		$("#btnCancel").click(function(){
			$("#txtUsername").val("");
			$("#txtPassword").val("");
		});
		
		$("#txtUsername, #txtPassword").keypress(function(e){
			if(e.which === 13){
				$.login();
			}
		});
		
		$.login = function(){			
			var username = $("#txtUsername").val();
			var password = $("#txtPassword").val();
			if(username === ""){
				$("#error").text("โปรดระบุ  Username");
				return false;
			}
			if(password === ""){
				$("#error").text("โปรดระบุ Password");
				return false;
			}
			
			$.ajax({
	            url : '${pageContext.request.contextPath}/backend/checkUser',
	            data: {username: username, password: password},
	            type: "POST",
	            success : function(data) {
					if(data.success){
						if(data.code === "0000"){
							window.location="${pageContext.request.contextPath}/backend/slides";
						}else{
							$("#error").text(data.msg);
						}
					}else{
						$("#error").text(data.msg);
					}
	            },
	            error : function(data){
					$("#error").text(data.msg);
	            }
	        });
		};
	});
</script>

<div style="text-align: center; margin-top: 20px">
    <div style="width: 300px; display: inline-block;">
		<div><span id="error" style="width: 450px; font-weight: bold; text-align: center; color: red">&nbsp;</span></div>
	    <div class="input-group">
			<span class="input-group-addon" id="basic-addon1">Username: </span>
			<input type="text" class="form-control" placeholder="Username" name="txtUsername" id="txtUsername" aria-describedby="basic-addon1">
		</div>
	    <div class="input-group" style="margin-top: 5px;">
			<span class="input-group-addon" id="basic-addon1">Password: </span>
			<input type="password" class="form-control" placeholder="Password" name="txtPassword" id="txtPassword" aria-describedby="basic-addon1">
		</div>
		<div style="margin-top: 5px">
			<button class="btn btn-primary" id="btnLogin">Login</button>
			<button class="btn btn-default" type="button" id="btnCancel">Cancel</button>
		</div>
	</div>
</div>
